package com.AdvancedDemos.FileAndIO;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Consumer;

public class 练习4 {
    public static void main(String[] args) throws IOException {
        /**
         GBK文本文件中存在如下诗句:
         4.尼玛还是富士康
         1.故人西辞富士康
         3.蓝翔毕业包分配
         2.为学技术去蓝翔
         请将如下诗句按照序号排序后,重新写回原GBK文件
         */


//        实现1();
        实现2();

    }

    private static void 实现2() throws IOException {
        //使用字节输入流读取数据
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\poem.txt"));
        byte[] bytes = bufferedInputStream.readAllBytes();
        bufferedInputStream.close();

        //以指定编码方式打开文件,等待写出
        BufferedWriter bw = new BufferedWriter(new FileWriter("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\poem.txt", Charset.forName("GBK")));//没有打开追加

        //使用String的构造函数以指定编码方式解码读到的字节数组
        /*String gbk = new String(bytes, "GBK");
        System.out.println(gbk);
        System.out.println("---------------");
        //以换行符作为分隔符切割字符串得到每行诗句
        String[] split = gbk.split("\r\n");
        for (String s : split) {
            System.out.println(s);
        }*/
        //↓直接替换为↓
        Arrays.stream(new String(bytes, "GBK").split("\r\n")).sorted(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int number1 = Integer.parseInt(o1.split("\\.")[0]);
                int number2 = Integer.parseInt(o2.split("\\.")[0]);
                return number1 - number2;
            }
        }).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                /*由于accept方法是重写的,所以无法用throws声明抛出异常,需要try-catch处理异常*/
                try {
                    bw.write(s);
                    bw.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        bw.close();
    }

    private static void 实现1() throws IOException {
        //使用GBK编码读取文件内容
        ArrayList<String> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\poem.txt", Charset.forName("GBK")));
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        list.sort(new Comparator<String>() {//调用ArrayList自身的sort方法,由比较器Comparator给出比较规则
            @Override
            public int compare(String o1, String o2) {
                int number1 = Integer.parseInt(o1.split("\\.")[0]);
                int number2 = Integer.parseInt(o2.split("\\.")[0]);
                return number1 - number2;
            }
        });
        System.out.println(list);//[1.故人西辞富士康, 2.为学技术去蓝翔, 3.蓝翔毕业包分配, 4.尼玛还是富士康]


        //写出文件,使用GBK写出
        BufferedWriter bw = new BufferedWriter(new FileWriter("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\poem.txt", Charset.forName("GBK")));//没有打开追加

        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
    }
}
